/***************************************************************************
 *   Copyright (C) 2010 by Oleg Goncharov  *
 *   $EMAIL$                           *                          
 *                                                                         *
 *   This file is part of ChessVision.                                     *
 *                                                                         *
 *   ChessVision is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef CFEATUREFIGURE_H
#define CFEATUREFIGURE_H

#include <string>

#include <cfiguredetector.h>

namespace Chess {

/**
Вычисляет параметры для применения алгоритмов классификации.

	@author Goncharov Oleg <goncharovoi@yandex.ru>
*/
class CFigureFeature : public CFigureDetector {
public:
	// Число классов наблюдаемых полей
	enum ClassModel { SIMPLE = 3, MEDIUM = 4, COMPLEX = 6 };
private:
	enum Operations { AVG, SDV, MAX, MIN, NOPERATIONS};
	enum Sources { IMG_B, IMG_G, IMG_R, GREY, GRAD , NSOURCES};
	struct HistoryBlock { 
		int block_offset, idx_in_block; 
		HistoryBlock() {};
		HistoryBlock(int offset, int idx) : block_offset(offset), idx_in_block(idx) {}
	};

	std::string config;
	int cols;
	int history_length;
	cv::Mat sources[NSOURCES];
	cv::Mat masks[10];
	ClassModel model;
	
	cv::Mat_<float> data;
	cv::Mat_<float> samples;
	cv::Mat_<float> classes;
	cv::Mat_<uchar> samples_idx;	
	std::map<float, HistoryBlock> history_idx;
private:
	void Prepare(const cv::Mat& img, const CBoardCells& _cells);
	void Release();
	void InitHistoryMap();
	
	void CalcFeatures(const CBoardCells& cells, int i, int j, cv::Mat_<float>& _data, int row);
protected:
	CFigureFeature(const std::string& conf_str, ClassModel model, int history_length = 0);
	
//TODO Убрать (?) доинициализаторы.
	void CalcAllFeatures(const cv::Mat& img, const CBoardCells& cells);
	void SetClasses(const CPosition& position);
	void UpdateHistory();
	
	int Cols() { return cols; };
	
	const cv::Mat_<float>& Samples() { return samples; }
	const cv::Mat_<uchar>& SamplesIdx() { return samples_idx; }
	const cv::Mat_<float>& Classes() { return classes; }
public:
	double InitialPositionAngle(const cv::Mat& img, const CBoardCells& cells);
};

}

#endif
